package com.code.petapp.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.code.petapp.R;
import com.code.petapp.activity.AboutActivity;
import com.code.petapp.activity.AccountActivity;
import com.code.petapp.activity.AdoptActivity;
import com.code.petapp.activity.ClaimActivity;
import com.code.petapp.activity.FosterActivity;
import com.code.petapp.activity.LoginActivity;
import com.code.petapp.activity.LostActivity;
import com.code.petapp.activity.MyClaimsActivity;
import com.code.petapp.activity.MyLostActivity;
import com.code.petapp.activity.OrderActivity;
import com.code.petapp.activity.SettingActivity;
import com.code.petapp.activity.SubImgActivity;
import com.code.petapp.activity.SubmitActivity;
import com.code.petapp.bean.BaseBean;
import com.code.petapp.bean.UserBean;
import com.code.petapp.databinding.FragmentMyBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.SPCommonUtils;
import com.code.petapp.util.UserUtils;

import java.util.ArrayList;

import io.reactivex.rxjava3.annotations.Nullable;

public class MyFragment extends Fragment {
    FragmentMyBinding binding;
    private boolean isGetData = false;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentMyBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        Glide.with(this).load(R.drawable.img_juan)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(binding.imgAvatar);
        imgListener();
        load();
        return view;
    }
    @Override
    public void onResume() {//每次重进页面都会刷新数据
        if (!isGetData) {
            //   这里可以做网络请求或者需要的数据刷新操作
            load();
            isGetData = true;
        }
        super.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        isGetData = false;
    }
    private void imgListener() {
        binding.linFoster.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), FosterActivity.class);
            startActivity(intent);
        });
        binding.linAdopt.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), AdoptActivity.class);
            startActivity(intent);
        });
        binding.linOrder.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), OrderActivity.class);
            startActivity(intent);
        });
        binding.linAccount.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), AccountActivity.class);
            Double account=Double.parseDouble(binding.tvBalance.getText().toString());
            intent.putExtra("account",account );
            startActivity(intent);
        });
        binding.linLost.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), MyLostActivity.class);
            startActivity(intent);
        });
        binding.linClaim.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), MyClaimsActivity.class);
            startActivity(intent);
        });
        binding.linSubImg.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), SubImgActivity.class);
            startActivity(intent);
        });
        binding.manager.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), SettingActivity.class);
            startActivity(intent);
        });
        binding.about.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), AboutActivity.class);
            startActivity(intent);
        });
    }
    private void load() {
        UserUtils.selectAll(SPCommonUtils.getInstance().getToken(), SPCommonUtils.getInstance().getUserId(), new HttpListener<>() {
            @SuppressLint("SetTextI18n")

            @Override
            public void success(ArrayList<UserBean> beanList) {
                UserBean userBean = beanList.get(0);
                binding.tvName.setText(userBean.getUsername());
                binding.tvBalance.setText(userBean.getAccount().toString());
                Glide.with(MyFragment.this)
                        .load(userBean.getAvatar())
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(binding.imgAvatar);
            }

            @Override
            public void error(int code, String error) {
                // 错误处理
                Toast.makeText(getActivity(), error , Toast.LENGTH_SHORT).show();
            }
        });
    }
}